package web_mapper

import (
	"context"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"sync"
	"transport/web/web_model"
)

type CarMapper struct {
	DB   *gorm.DB
	Ctx  context.Context
	Lock *sync.RWMutex
}

func (mapper *CarMapper) Create(m *web_model.TransCar) (ret *web_model.TransCar, err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Create(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *CarMapper) Upsert(m *web_model.TransCar) (ret *web_model.TransCar, err error) {
	if err = mapper.DB.WithContext(mapper.Ctx).Clauses(clause.OnConflict{
		Columns: []clause.Column{{Name: "customerId"}},
		DoUpdates: clause.AssignmentColumns([]string{"fzrq", "lxr", "lxrdh", "qylxdm",
			"qymc", "shxydm", "sylxdm", "xkzbh", "yxqz"}),
	}).Create(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *CarMapper) Update(m *web_model.TransCar) (ret *web_model.TransCar, err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_model.TransCar{}).Where("id = ?", m.ID).Updates(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *CarMapper) CreateOrUpdate(m *web_model.TransCar) (ret *web_model.TransCar, err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(m).Save(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *CarMapper) QueryById(id int64) (m *web_model.TransCar, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_model.TransCar{}).Where("id = ?", id).First(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *CarMapper) QueryByIds(ids []int64) (m []*web_model.TransCar, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_model.TransCar{}).Where("id IN ?", ids).Find(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *CarMapper) DeleteById(id int64) (err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id = ?", id).Delete(&web_model.TransCar{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *CarMapper) DeleteByIds(ids []int64) (err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id IN ?", ids).Delete(&web_model.TransCar{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *CarMapper) Page(pageIndex, pageSize int, sn, carType string) (items []*web_model.TransCar, count int64, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx)
	if sn != "" {
		table = table.Where("sn like ?", "%"+sn+"%")
	}
	if carType != "" {
		table = table.Where("type =  ?", carType)
	}
	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&items).Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}

func (mapper *CarMapper) List() (items []*web_model.TransCar, count int64, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx)
	if err := table.Find(&items).Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}
